#include "factory.h"

int factoryInit(factory_t *f,int threadNum)//工厂初始化
{
    bzero(f,sizeof(factory_t));
    //存线程id的空间
    f->thidArr=(pthread_t *)calloc(threadNum,sizeof(pthread_t));
    f->threadNum=threadNum;//线程数目
    pthread_cond_init(&f->cond,NULL);//条件变量初始化
    pthread_mutex_init(&f->taskQueue.mutex,NULL);//队列（锁）的初始化
    f->exitFlag=0;//退出标志的初始化
    return 0;
}

int factoryStart(factory_t *f)//启动线程
{
    int i,ret;
    for(i=0;i<f->threadNum;i++)
    {
        //threadFunc是线程函数
        ret=pthread_create(f->thidArr+i,NULL,threadFunc,f);
        THREAD_ERR_CHECK(ret,"pthread_create");   
    }
    return 0;
}

//注册监控某个描述符是否可读
int epoll_add(int epfd,int fd)
{
    struct epoll_event event;
    event.data.fd=fd;
    event.events=EPOLLIN;//可读
    int ret=epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&event);
    ERROR_CHECK(ret,-1,"epoll_ctl");
    return 0;
}

//解除监控某个描述符
int epoll_del(int epfd,int fd)
{
    struct epoll_event event;
    event.data.fd=fd;
    event.events=EPOLLIN;
    int ret=epoll_ctl(epfd,EPOLL_CTL_DEL,fd,&event);
    ERROR_CHECK(ret,-1,"epoll_ctl");
    return 0;
}

//修改路径
void modify_path_slash(char *path)
{
    int len=strlen(path);
    //寻找倒数第二个斜杠的位置
    int second_last_slash;
    for(int i=len-2;i>=0;i--)
    {
        if(path[i]=='/')
        {
            second_last_slash=i;
            break;
        }
    }
    path[second_last_slash+1]=0;
}